package com.example.myapplication.data;

import android.app.Application;
import androidx.lifecycle.LiveData;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.util.Log;

public class RecordRepository {
    private static final String TAG = "RecordRepository";
    private RecordDao recordDao;
    private ExecutorService executorService;

    public RecordRepository(Application application) {
        AppDatabase db = AppDatabase.getDatabase(application);
        recordDao = db.recordDao();
        executorService = Executors.newSingleThreadExecutor();
    }

    public void insert(Record record) {
        Log.d(TAG, "Inserting record: " + record.toString());
        try {
            // 插入记录并获取生成的ID
            long id = recordDao.insertAndGetId(record);
            Log.d(TAG, "Record inserted with ID: " + id);
            
            // 验证插入是否成功
            List<Record> allRecords = recordDao.getAllRecordsDirectly();
            Log.d(TAG, "Current records in database: " + allRecords.size());
            for (Record r : allRecords) {
                Log.d(TAG, "Record in DB: " + r.toString());
            }
        } catch (Exception e) {
            Log.e(TAG, "Error inserting record: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void update(Record record) {
        try {
            Log.d(TAG, "Updating record: " + record.toString());
            recordDao.update(record);
            Log.d(TAG, "Record updated successfully");
        } catch (Exception e) {
            Log.e(TAG, "Error updating record: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public void delete(Record record) {
        try {
            Log.d(TAG, "Deleting record with ID: " + record.getId());
            recordDao.deleteById(record.getId());
            Log.d(TAG, "Record deleted successfully");
            
            // 验证删除结果
            List<Record> remainingRecords = recordDao.getAllRecordsDirectly();
            Log.d(TAG, "Remaining records: " + remainingRecords.size());
        } catch (Exception e) {
            Log.e(TAG, "Error deleting record: " + e.getMessage());
            e.printStackTrace();
        }
    }

    public LiveData<List<Record>> getAllRecords() {
        return recordDao.getAllRecords();
    }

    public List<Record> getAllRecordsDirectly() {
        try {
            List<Record> records = recordDao.getAllRecordsDirectly();
            Log.d(TAG, "Retrieved " + records.size() + " records directly");
            return records;
        } catch (Exception e) {
            Log.e(TAG, "Error getting records directly: " + e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public LiveData<List<Record>> getRecordsByDateRange(long startDate, long endDate) {
        Log.d(TAG, "Querying records from " + new Date(startDate) + " to " + new Date(endDate));
        return recordDao.getRecordsByDateRange(startDate, endDate);
    }

    public LiveData<Double> getTotalExpenseByDateRange(long startDate, long endDate) {
        return recordDao.getTotalExpenseByDateRange(startDate, endDate);
    }

    public LiveData<Double> getTotalIncomeByDateRange(long startDate, long endDate) {
        return recordDao.getTotalIncomeByDateRange(startDate, endDate);
    }

    public void deleteAllRecords() {
        try {
            Log.d(TAG, "Deleting all records");
            recordDao.deleteAllRecords();
            Log.d(TAG, "All records deleted successfully");
        } catch (Exception e) {
            Log.e(TAG, "Error deleting all records: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 